<?php
class Core_Data_UserData {
	
	public $database;
	
	public function __construct(){
		$this->database = new Core_Driver_MysqlImproved();
	}
	
	public function getUserData(array $postVars){
		try {
			
			if($this->checkUser($postVars) == TRUE){
				return $this->getUserDataFromDatabase($postVars['name'], $postVars['password']);
			}
			else {
				throw new Exception("User does not exists!");
			}
			
		}
		catch(Exception $e){
			echo $e->getMessage();
		}
	}
	
	public function getUserDataFromDatabase($name, $password){
		$this->database->connect();
		$name = $this->database->escape($name);
		$password = $this->database->escape($password);
		$this->database->prepare(
				"
				SELECT * FROM users WHERE users_username = '$name' AND users_password = '$password' LIMIT 1	;
				"
		);
		$this->database->query();
		$userData = $this->database->fetch('array');
		$_SESSION = $userData;
	}
	
	public function checkUser(array $postVars){
		$this->database->connect();
		$name = $postVars['name'];
		$name = $this->database->escape($name);
		$password = $postVars['password'];
		$password = $this->database->escape($password);
		$this->database->prepare(
				"
				SELECT * FROM users WHERE users_username = '$name' AND users_password = '$password' LIMIT 1
				;
				"
		);
		$this->database->query();
		$userData = $this->database->fetch('array');
		if(!$this->userData['users_username'] == NULL){
			return FALSE;
		}
		$_SESSION = $userData;
	}
}